<template>
  <!-- 弹出框 -->
  <el-dialog
    title="编辑部门"
    :visible="dialogVisible"
    width="50%"
    center
    @close="btnNoOk"
  >
    <el-form ref="form" :model="form" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" placeholder="必填 1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" placeholder="必填 1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择">
          <el-option v-for="item in employeeList" :key="item.id" :label="item.username" :value="item.username" />

        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" placeholder="必填 1-300个字符" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="btnNoOk">取 消</el-button>
      <el-button type="primary" @click="btnIsOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getEmployeeList } from '@/api/employees'
import { addDepartment } from '@/api/department'

export default {
  props: {
    dialogVisible: {
      type: Boolean,
      default: false
    },
    departmentObj: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      // 员工列表
      employeeList: {},
      // 表单数据
      form: {
        code: '', // 部门编码
        introduce: '', // 介绍
        manager: '', // 负责人名称
        name: '', // 部门名称
        pid: ''
      },
      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      }
    }
  },

  created() {
    // 获取员工列表
    getEmployeeList({ page: 1, size: 100 }).then(res => {
      this.employeeList = res.rows
    })
  },
  methods: {

    // 取消按钮
    btnNoOk() {
      // 清理数据
      this.form = {
        code: '', // 部门编码
        introduce: '', // 介绍
        manager: '', // 负责人名称
        name: '', // 部门名称
        pid: ''
      }
      // 清除错误
      this.$refs.form.clearValidate()
      //   关闭弹窗
      this.$emit('update:dialogVisible', false)
    },
    // 确认新增或编辑
    async btnIsOk() {
      // 判断是否有id，有id就是添加子部门，没有就是新增
      try {
        // 验证表单
        await this.$refs.form.validate()
        // 判断是否添加子部门
        if (this.departmentObj.id) {
          this.form.pid = this.departmentObj.id
        }
        await addDepartment(this.form)
        this.$message.success('操作成功')
        this.$emit('addOrCompileOk')
      } catch (error) {
        this.$message.error('操作失败，请检查必填项')
      }
    }

  }
}
</script>

<style>

</style>
